package hapi.send;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.TickerBehaviour;
import jade.lang.acl.ACLMessage;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.sound.midi.Transmitter;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v23.message.ADT_A04;
import ca.uhn.hl7v2.model.v23.message.ADT_A08;
import ca.uhn.hl7v2.model.v24.message.ADT_A01;
import ca.uhn.hl7v2.model.v24.message.ADT_A03;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.model.v24.segment.PID;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.GenericParser;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.parser.PipeParser;
import ca.uhn.hl7v2.util.Terser;

public class SendBehaviour extends TickerBehaviour {

	private Agent agent;
	volatile List<AID> providerList;

	public SendBehaviour(SendAgent agent, int period) {
		super(agent, period);
		this.agent = agent;
		providerList = agent.getProviderList();
	}

	@Override
	public void onTick() {
		
		ACLMessage ms = new ACLMessage(ACLMessage.INFORM);
		
		Random r = new Random();

		ms.addReceiver(providerList.get(r.nextInt(providerList.size())));
		
		if(providerList.size()>0){
			update(ms);
		}
	}
	
	private void admission(ACLMessage ms){
		ADT_A01 adt = new ADT_A01();
		
		MSH mshSegment = adt.getMSH();
		try {
			mshSegment.getFieldSeparator().setValue("|");
			mshSegment.getEncodingCharacters().setValue("^~\\&");
			mshSegment.getSendingApplication().getNamespaceID().setValue("HIS");
			mshSegment.getSendingFacility().getNamespaceID().setValue("RIH");
			mshSegment.getReceivingApplication().getNamespaceID().setValue("EKG");
			mshSegment.getReceivingFacility().getNamespaceID().setValue("EKG");
			mshSegment.getDateTimeOfMessage().getTimeOfAnEvent().setValue("201206141500");
			
			mshSegment.getMessageType().getMessageType().setValue("ADT");
			mshSegment.getMessageType().getTriggerEvent().setValue("A01");
			
			mshSegment.getProcessingID().getProcessingID().setValue("P");
			mshSegment.getVersionID().getVersionID().setValue("2.4");
		} catch (DataTypeException e) {
			e.printStackTrace();
		}

		PID pid = adt.getPID(); 
		try {
			pid.getPatientName(0).getFamilyName().getSurname().setValue("Rodrigues");
			pid.getPatientName(0).getGivenName().setValue("Pedro");
			pid.getPatientIdentifierList(0).getID().setValue("10101616");
			try {
				pid.getPatientAddress(0).getStreetAddress().parse("Amarante");
			} catch (HL7Exception e) {
				e.printStackTrace();
			}
			pid.getAdministrativeSex().setValue("M");
			
		} catch (DataTypeException e) {
			e.printStackTrace();
		}
		
		Parser parser = new PipeParser();
		String encodedMessage = null;
		try {
			encodedMessage = parser.encode(adt);
		} catch (HL7Exception e) {
			e.printStackTrace();
		}
		
		ms.setContent(encodedMessage);
		ms.setOntology("HL7Ontology");
		
		agent.send(ms);
		//System.out.println(System.currentTimeMillis());
//		agent.send(ms);
//		agent.send(ms);
//		agent.send(ms);
//		agent.send(ms);
//		agent.send(ms);
//		agent.send(ms);
//		agent.send(ms);
//		agent.send(ms);
//		agent.send(ms);
		stop();
	}
	
	private void discharge(ACLMessage ms){
		ADT_A03 adt = new ADT_A03();
		
		MSH mshSegment = adt.getMSH();
		try {
			mshSegment.getFieldSeparator().setValue("|");
			mshSegment.getEncodingCharacters().setValue("^~\\&");	
			mshSegment.getMessageType().getMessageType().setValue("ADT");
			mshSegment.getMessageType().getTriggerEvent().setValue("A03");
			mshSegment.getVersionID().getVersionID().setValue("2.4");
		} catch (DataTypeException e) {
			e.printStackTrace();
		}
		
		PID pid = adt.getPID(); 
		try {
			pid.getPatientName(0).getFamilyName().getSurname().setValue("Rodrigues");
			pid.getPatientName(0).getGivenName().setValue("Pedro");
			pid.getPatientIdentifierList(0).getID().setValue("10101616");
		} catch (DataTypeException e) {
			e.printStackTrace();
		}

		Parser parser = new PipeParser();
		String encodedMessage = null;
		try {
			encodedMessage = parser.encode(adt);
		} catch (HL7Exception e) {
			e.printStackTrace();
		}
		
		ms.setContent(encodedMessage);
		ms.setOntology("HL7Ontology");
		agent.send(ms);
		stop();
	}
	
	private void update(ACLMessage ms){
		ADT_A08 adt = new ADT_A08();
		
		ca.uhn.hl7v2.model.v23.segment.MSH mshSegment = adt.getMSH();
		try {
			mshSegment.getFieldSeparator().setValue("|");
			mshSegment.getEncodingCharacters().setValue("^~\\&");	
			mshSegment.getMessageType().getMessageType().setValue("ADT");
			mshSegment.getMessageType().getTriggerEvent().setValue("A08");
			mshSegment.getVersionID().setValue("2.4");
		} catch (DataTypeException e) {
			e.printStackTrace();
		}
		
		ca.uhn.hl7v2.model.v23.segment.PID pid = adt.getPID(); 
		try {
			try {
				pid.getPatientAddress(0).getStreetAddress().parse("Braga");
			} catch (HL7Exception e) {
				e.printStackTrace();
			}
			pid.getSetIDPatientID().setValue("10101616");
		} catch (DataTypeException e) {
			e.printStackTrace();
		}

		Parser parser = new PipeParser();
		String encodedMessage = null;
		try {
			encodedMessage = parser.encode(adt);
		} catch (HL7Exception e) {
			e.printStackTrace();
		}
		
		ms.setContent(encodedMessage);
		ms.setOntology("HL7Ontology");
		agent.send(ms);
		stop();
	}
	
	private void register(ACLMessage ms){
		ADT_A04 adt = new ADT_A04();
		
		ca.uhn.hl7v2.model.v23.segment.MSH mshSegment = adt.getMSH();
		try {
			mshSegment.getFieldSeparator().setValue("|");
			mshSegment.getEncodingCharacters().setValue("^~\\&");
			mshSegment.getMessageType().getMessageType().setValue("ADT");
			mshSegment.getMessageType().getTriggerEvent().setValue("A04");
			
			mshSegment.getProcessingID().getProcessingID().setValue("P");
			mshSegment.getVersionID().setValue("2.3");
		} catch (DataTypeException e) {
			e.printStackTrace();
		}
		
		ca.uhn.hl7v2.model.v23.segment.PID pid = adt.getPID(); 
		try {
			pid.getPatientName().getFamilyName().setValue("Barroso");
			pid.getPatientName().getGivenName().setValue("Alberto");
			try {
				pid.getPatientAddress(0).getStreetAddress().parse("Amarante");
			} catch (HL7Exception e) {
				e.printStackTrace();
			}
			pid.getSex().setValue("M");
			
		} catch (DataTypeException e) {
			e.printStackTrace();
		}
		
		Parser parser = new PipeParser();
		String encodedMessage = null;
		try {
			encodedMessage = parser.encode(adt);
		} catch (HL7Exception e) {
			e.printStackTrace();
		}
		
		ms.setContent(encodedMessage);
		ms.setOntology("HL7Ontology");
		
		agent.send(ms);
		//System.out.println(System.currentTimeMillis());
		stop();
	}

}
